package com.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.Result;
import com.common.utils.StringUtil;
import com.entity.SharesInform;
import com.mapper.SharesInformMapper;
import com.service.SharesInformService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vo.SharesInformVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wty
 * @since 2022-01-19
 */
@Service
public class SharesInformServiceImpl extends ServiceImpl<SharesInformMapper, SharesInform> implements SharesInformService {


    @Override
    public Result getInformList(HttpServletRequest request) {

        String type = request.getParameter("type");
        String code = request.getParameter("code");
        String time = request.getParameter("time");

        Wrapper<SharesInform> wr=new EntityWrapper<>();
        if(!StringUtil.isEmpty(type)){
            wr.eq("type",type);
        }
        if(!StringUtil.isEmpty(code)){
            wr.eq("code",code);
        }
        if(!StringUtil.isEmpty(time)){
            wr.like("time",time);
        }
        wr.orderBy("id");
        List<SharesInform> sharesInforms = this.selectList(wr);
        Map<String,List<SharesInform>> map=new HashMap<>();
        if(!CollectionUtils.isEmpty(sharesInforms)){

            for (SharesInform inform : sharesInforms) {
                String voCode = inform.getCode();
                List<SharesInform> voList = map.get(voCode);
                if(!CollectionUtils.isEmpty(voList)){
                    voList.add(inform);
                }else{
                    List<SharesInform> toList=new ArrayList<>();
                    toList.add(inform);
                    map.put(voCode,toList);
                }
            }
        }

        int index=0;
        List<SharesInformVo> sharesInformVos=new ArrayList<>();
        if(!CollectionUtils.isEmpty(map)){
            for (String key : map.keySet()) {
                SharesInformVo sharesInformVo=new SharesInformVo()  ;
                List<SharesInform> voList = map.get(key);
                sharesInformVo.setInforms(voList);
                sharesInformVo.setCode(key);
                sharesInformVo.setId(index++);
                sharesInformVo.setName(voList.get(0).getName());
                sharesInformVos.add(sharesInformVo);
            }
        }
        return Result.success(sharesInformVos);
    }
}
